<?php
//声明命名空间
namespace Admin\Controller;

use Admin\Model\ArticleModel;
use Frame\Libs\BaseController;
use Admin\Model\CategoryModel;

//定义最终的文章控制器类并继承基础类
final class ArticleController extends BaseController
{
    public function index()
    {
        //验证是否登录
        $this->denyAccess();
        //无限极分类数据
        $Categorys = CategoryModel::getInstance()->CategoryList(
            CategoryModel::getInstance()->fetchAll()
        );

		//构建查询条件
		$where = "2>1";
		if(!empty($_REQUEST["category_id"])) $where .= " AND category_id=".$_REQUEST["category_id"];
		if(!empty($_REQUEST["keyword"])) $where .= " AND title LIKE '%".$_REQUEST["keyword"]."%'";

		
		//构造分页类参数
		$page = isset($_GET["page"]) ? $_GET["page"] : 1;
		$pagesize = 3;
		$startpage = ($page-1) * $pagesize;
		$records = ArticleModel::getInstance()->rowCount($where);
		$params = [
			"c" => CONTROLLER,
			"a" => ACTION,
		];

		//如果条件存在,则添加链接地址条件
		if(!empty($_REQUEST["category_id"])) $params["category_id"] = $_REQUEST["category_id"];
		if(!empty($_REQUEST["keyword"])) $params["keyword"] = $_REQUEST["keyword"];

		//获取连表查询的文章分页数据
        $Articles = ArticleModel::getInstance()->Article($where,$startpage,$pagesize);

		//创建分类对象
		$pager = new \Frame\Vendor\Pager($records,$pagesize,$page,$params);
		$pagestr = $pager->showPage();

		//向视图赋值
        $this->smarty->assign([
            "categorys" => $Categorys,
            "article" => $Articles,
			"page" =>	$pagestr
        ]);
        $this->smarty->display("Article/index.html");
    }

    public function del()
    {
        //验证是否登录
        $this->denyAccess();
        $id = $_GET["id"];
        $result = ArticleModel::getInstance()->del($id);
        if($result){
            $this->jump("id={$id}的数据删除成功","?c=Article");
        }else{
            $this->jump("id={$id}的数据删除成功","?c=Article");
        }
    }
    public function update()
    {
        //显示文章修改页面方法
        //验证是否登录
        $this->denyAccess();
        $id = $_GET["id"];
        $Article = ArticleModel::getInstance()->fetchOne("id=$id");
        $this->smarty->assign("article",$Article);
        $this->smarty->display("Article/update.html");
    }
    public function edit()
    {
        $id = $_POST["id"];
        $title = $_POST["title"];
        $orderby = $_POST["orderby"];
        $contents = $_POST["content"];
        $result = ArticleModel::getInstance()->edit("title='$title',orderby='$orderby',content='$contents'",$id);
        if($result){
            $this->jump("id={$id}的数据修改成功","?c=Article");
        }else{
            $this->jump("id={$id}的数据修改失败","?c=Article");
        }
    }
    public function add()
    {
        //验证是否登录
        $this->denyAccess();
        //无限极分类数据
        $Categorys = CategoryModel::getInstance()->CategoryList(
            CategoryModel::getInstance()->fetchAll()
        );
        $this->smarty->assign("categorys",$Categorys);
        $this->smarty->display("Article/add.html");
    }
    public function insert()
    {
        //验证是否登录
        $this->denyAccess();
        $data["title"]        = $_POST["title"];
        $data["orderby"]      = $_POST["orderby"];
        $data["category_id"] = $_POST["category_id"];
        $data["top"]          =  isset($_POST["top"]) ? 1 : 0 ;
        $data["content"]      = $this->filter($_POST["content"]);
        $data["user_id"]      = $_SESSION["id"];
        $data["addate"]       =  time();
        $Article = ArticleModel::getInstance()->insert($data);
        if($Article){
            $this->jump("文章添加成功","?c=Article");
        }else{
            $this->jump("文章添加失败","?c=Article");
        }
    }
}